﻿package KK.Controls.Tips{
	import flash.display.Bitmap;
	import flash.display.BitmapData;
	import flash.geom.Point;
	import flash.geom.Rectangle;
	public class Box extends Bitmap{
		public static const WIDTH:int=12;
		public static const HEIGHT:int=12;
		public static const MIN_WIDTH:int=24;
		public static const MIN_HEIGHT:int=24;
		
		
		static private var AUTO_INCREMENT:int=0;
		static private function get AI():int{
			return AUTO_INCREMENT++;
		}
		////////////////////////////////////////////
		static private const B_LT:int=AI;//左上
		static private const B_MT:int=AI;//中上
		static private const B_RT:int=AI;//右上
		static private const B_ML:int=AI;//中间左边
		static private const B_MM:int=AI;//正中
		static private const B_MR:int=AI;//中右
		static private const B_BL:int=AI;//下左
		static private const B_BM:int=AI;//下中
		static private const B_BR:int=AI;//下右
		
		static private const B_MAX:int=AI;// 长度
		
		private var box:Vector.<BitmapData>;
		
		private var _width:int;
		private var _height:int;
		public function Box():void{
			init();
		}
		
		private function init():void{
			this.width=MIN_WIDTH;
			this.height=MIN_HEIGHT;
		}
		
		
		public function SetBitmap(bm:BitmapData,W_Lump:int,H_Lump:int):void{
			box=new Vector.<BitmapData>(B_MAX,true);
			var point:Point=new Point(0,0);
			var rect:Rectangle=new Rectangle(0,0,0,0);
			
//			var W_Lump:int=WIDTH;
//			var H_Lump:int=HEIGHT;
			
			//左上角
			var bd:BitmapData=new BitmapData(W_Lump,H_Lump);
			rect.x=0;
			rect.y=0;
			rect.width=WIDTH;
			rect.height=HEIGHT;
			bd.copyPixels(bm,rect,point);
			box[B_LT]=bd;
			
			//上
			bd=new BitmapData(W_Lump,H_Lump);
			rect.x=WIDTH;
			rect.y=0;
			bd.copyPixels(bm,rect,point); 
			box[B_MT]=bd;
			
			//右上角
			bd=new BitmapData(W_Lump,H_Lump);
			rect.x=bm.width-WIDTH;
			rect.y=0;
			bd.copyPixels(bm,rect,point);
			box[B_RT]=bd;
			
			//中左角
			bd=new BitmapData(W_Lump,H_Lump);
			rect.x=0;
			rect.y=HEIGHT;
			bd.copyPixels(bm,rect,point);
			box[B_ML]=bd;
			
			//中中角
			bd=new BitmapData(W_Lump,H_Lump);
			rect.x=WIDTH;
			rect.y=HEIGHT;
			bd.copyPixels(bm,rect,point);
			box[B_MM]=bd;
			
			//中右角
			bd=new BitmapData(W_Lump,H_Lump);
			rect.x=bm.width-WIDTH;
			rect.y=HEIGHT;
			bd.copyPixels(bm,rect,point);
			box[B_MR]=bd;
			
			//下左角
			bd=new BitmapData(W_Lump,H_Lump);
			rect.x=0;
			rect.y=bm.height-HEIGHT;
			bd.copyPixels(bm,rect,point);
			box[B_BL]=bd;
			
			//下中角
			bd=new BitmapData(W_Lump,H_Lump);
			rect.x=WIDTH;
			rect.y=bm.height-HEIGHT;
			bd.copyPixels(bm,rect,point);
			box[B_BM]=bd;
			
			//下右角
			bd=new BitmapData(W_Lump,H_Lump);
			rect.x=bm.width-WIDTH;
			rect.y=bm.height-HEIGHT;
			bd.copyPixels(bm,rect,point);
			box[B_BR]=bd;
			
			//重绘
			ReDraw(this._width,this._height);
		}
		
		//ReDraw 
		//_width  width;
		//_height height;
		protected function ReDraw(_width:int,_height:int):void
		{
			if(!box)
			{
				return ;
			} 
			var bd:BitmapData=new BitmapData(_width,_height,true,0);
			var rect:Rectangle=new Rectangle();
			var point:Point=new Point();
			
			var i:int=0;
			var j:int=0;
			var H:int=0;
			var V:int=0;
			var totalH:int=_width/WIDTH;
			var totalV:int=_height/HEIGHT;
			
			rect.y=rect.x=0;
			rect.width=WIDTH;
			rect.height=HEIGHT;
			
			for(i=0;i<totalV;i++)
			{
				point.y=i*WIDTH;
				V=(i==0||i==totalV-1?i==0?0:2:1);
				
				for(j=0;j<totalH;j++)
				{
					point.x=j*WIDTH;
					H=(j==0||j==totalH-1?j==0?0:2:1);
					bd.copyPixels(box[V*3+H],rect,point);
				}
			}
			 
			 
			this.bitmapData=bd; 
		}
		//set Box Width
		override public function set width(value:Number):void{
			//super.width=value;
			this._width=int(value);
			ReDraw(this._width,this._height);
			
		}
		//get Box width;
		override public function get width():Number{
			
			return this._width;
		}
		//set box height
		override public function set height(value:Number):void{
			//super.height=value;
			this._height=int(value);
			ReDraw(this._width,this._height);
			
		}
		//get box height
		override public function get height():Number{
			return this._height;
		}
		
	}
}